#include <bits/stdc++.h>

using namespace std;

int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
bool flag = true;
int area = 0;

void dfs(vector<vector<int>> &graph, vector<vector<bool>> &visited, int row, int col)
{
    if (graph[row][col] == 0 || visited[row][col] == true)
    {
        return;
    }
    if (row == 0 || row == (int)graph.size() - 1 || col == 0 || col == (int)graph[0].size() - 1)
    {
        flag = false;
    }
    area++;
    visited[row][col] = true;
    for (int i = 0; i < 4; i++)
    {
        int next_row = row + dir[i][0];
        int next_col = col + dir[i][1];
        if (next_row < 0 || next_row >= (int)graph.size() || next_col < 0 || next_col >= (int)graph[0].size())
        {
            continue;
        }
        dfs(graph, visited, next_row, next_col);
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(m, 0));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> graph[i][j];
        }
    }
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    int area_sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (graph[i][j] == 1 && visited[i][j] == false)
            {
                flag = true;
                area = 0;
                dfs(graph, visited, i, j);
                if (flag == true)
                {
                    area_sum += area;
                }
            }
        }
    }
    cout << area_sum << endl;
    return 0;
}